from flask import request
import math
class Page:

    currentPageKeyWords = 'current'
    pageSizeKeyWords = 'pageSize'
    DefaultCurrentPage = '1'
    DefaultPageSize = '6'
    pageMaxSize = 100
    
    def __init__(self,result):
        self.result = result
        self.page = request.args.get(self.currentPageKeyWords,self.DefaultCurrentPage)
        self.size = request.args.get(self.pageSizeKeyWords,self.DefaultPageSize)

        if self.page.isdigit():
            self.page = int(self.page)
        else:
            self.page = int(self.DefaultCurrentPage)
        if self.size.isdigit():
            self.size= int(self.size)
        else:
            self.size = int(self.DefaultPageSize)
    
    def get_page_data(self):
        if self.page <= 0:
            return []
        elif self.size <=0:
            return []

        if self.size > self.pageMaxSize:
            return []
        return self.result[(self.page -1)  * self.size:self.page * self.size + 1]


    @property
    def data(self):
        return self.get_page_data()

    def get_page_info(self):
        return {
            'total':len(self.result),
            'current':self.page,
            'pageSize':self.size,
            'totalPage':math.ceil(len(self.result) / self.size)
        }
    

    @property
    def page_info(self):
        return self.get_page_info()
        